<template>
	<view>
		<view class="content">
			<view class="meal-mess">
				<view class="meal-title">取餐信息</view>
				<view class="meal-address" @click="chooseStore()">
					<view class="store-address">
						<view class="meal-icons">
							<image style="width: 100%; height: 100%;" :src="icons.locate"></image>
						</view>
						<view class="meal-store">{{ currentEqiupment.eqiupment_name }}</view>
					</view>
					<view class="icon-right">
						<image style="width: 100%; height: 100%;" :src="icons.right"></image>
					</view>
				</view>
				<view class="take-content">
					<view class="take-title">取餐时间</view>
					<view class="take-time">中午 {{ time }} 前</view>
				</view>
			</view>
			<view class="pay-way">
				<view class="pay-title">支付方式</view>
				<view class="choose-way">
					<view class="choose-title">微信支付</view>
					<view class="choose-right">
						<image style="width: 100%; height: 100%;" :src="icons.right"></image>
					</view>
				</view>
			</view>
			<view class="meal-list">
				<view class="meal-title">餐品信息</view>
				<view class="meal-content">
					<template v-for="(items, index) in goodsCart">
						<view class="meal-list-items" v-if="items.number > 0" :key="index">
							<view class="meal-list-img">
								<image style="width: 100%; height: 100%;" mode="aspectFit" :src="items.main_img"></image>
							</view>
							<view class="meal-list-content">
								<view class="meal-items">
									<view class="meal-list-name">{{ items.goods_name }}</view>
									<view class="meal-list-price">￥{{ items.price }}</view>
								</view>
								<view class="meal-list-time">{{ items.time }} <span class="count-number">x{{ items.number }}</span></view>
							</view>
						</view>
					</template>
				</view>
			</view>
		</view>
		<view class="flex-content">
			<view class="defray">
				<view class="defray-count">合计：¥ {{ allMoney }}</view>
				<view class="defray-button" @click="checkOrder()">支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				time: '14:00',
				icons: {
					locate: '/static/images/icons/icon_dw@3x.png',
					right: '/static/images/icons/icon_next@3x.png'
				},
			};
		},
		computed: {
			...mapGetters([
				'goodsCart', 'currentEqiupment'
			]),
			allMoney: function() {
				const data = this.goodsCart;
				let money = 0;
				data.forEach(element => {
					money += element.number * element.price;
				})
				return money.toFixed(2);
			}
		},
		created() {
			const _this = this;
			let token = "";
			uni.getStorage({
				key: 'uerInfo',
				success: function(res) {
					_this.keepLogin(res.data.token)
				}
			});
		},
		methods: {
			keepLogin(token) {
				const path = {
					app: "wx",
					api: "api_wx_user_keepLogin",
					version: "1.0",
					type: "1",
					data: {
						token: token
					}
				}
				this.sendSock(path)
			},
			funchooseStore() {
				const _this = this;
				uni.navigateTo({
					url: '/pages/meal-picker/list' + '?' + _this.version
				})
			},
			checkOrder() {
				const _this = this;
				uni.getStorage({
					key: 'uerInfo',
					success: function(res) {
						if (res.data.mobile) {
							_this.createOrder()
						} else {
							uni.navigateTo({
								url: '/pages/personal/bind' + '?' + _this.version
							})
						}
					}
				})
			},
			createOrder() {
				const path = {
					app: "wx",
					api: "api_wx_order_createOrder",
					version: "1.0",
					type: 2,
					data: {
						equipment_id: this.currentEqiupment.eqiupment_id,
						pay_type: 1,
						goods_list: this.goodsCart.length > 0 ? this.goodsCart : ""
					}
				};
				this.sendSock(path)
			},
			sendSock(path) {
				if (this.$store.state.socketOpen) {
					uni.sendSocketMessage({
						data: JSON.stringify(path)
					});
				} else {
					this.$store.state.socketMsgQueue.push(JSON.stringify(path));
				}
				const _this = this
				uni.onSocketMessage(function(res) {
					_this.getSock(res.data)
				});
			},
			getSock(res) {
				if (res !== '{"type":"ping"}') {
					const resj = JSON.parse(res);
					if (resj.code === -1) {
						uni.showToast({
							icon: "none",
							title: resj.message,
							duration: 2000
						});
					} else {
						if (resj.api === "api_wx_user_login") {
							this.$store.commit('login', resj.data);
							this.temporary()
						} else if (resj.api === "api_wx_user_keepLogin") {
							this.$store.commit('login', resj.data);
							this.temporary()
						} else if (resj.api === "api_wx_order_createOrder") {
							const item = {
								price: resj.data.money,
								token: resj.data.token
							}
							this.pay(item);
						}
					}
				}
			}
		}
	};
</script>

<style>
	.content {
		padding: 0 30rpx;
	}

	.meal-mess {
		margin-top: 40rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 0px 30px 0px rgba(0, 0, 0, 0.1);
		border-radius: 10px;
		padding: 30rpx 20rpx;
	}

	.meal-title {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: rgba(34, 34, 34, 1);
	}

	.meal-address {
		margin-top: 36rpx;
		justify-content: space-between;
	}

	.store-address,
	.icon-right,
	.meal-icons,
	.take-content,
	.meal-address,
	.pay-way,
	.choose-way,
	.choose-right,
	.meal-list-items,
	.defray {
		display: flex;
		align-items: center;
	}

	.meal-icons {
		width: 30rpx;
		height: 30rpx;
	}

	.meal-store {
		margin-left: 12rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(34, 34, 34, 1);
		line-height: 1;
	}

	.icon-right {
		width: 16rpx;
		height: 30rpx;
	}

	.take-content {
		margin-top: 26rpx;
		border-top: 1rpx solid #e0e0e0;
		padding-top: 26rpx;
		justify-content: space-between;
	}

	.take-title {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
	}

	.take-time {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(38, 141, 255, 1);
	}

	.pay-way {
		margin-top: 38rpx;
		height: 96rpx;
		justify-content: space-between;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 0px 30px 0px rgba(0, 0, 0, 0.1);
		border-radius: 10px;
		padding: 0 20rpx;
	}

	.pay-title {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
	}

	.choose-title {
		margin-right: 18rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(34, 34, 34, 1);
	}

	.choose-right {
		width: 16rpx;
		height: 30rpx;
	}

	.meal-list {
		margin-top: 60rpx;
	}

	.meal-list-items {
		margin-bottom: 60rpx;
	}

	.meal-content {
		margin-top: 40rpx;
	}

	.meal-list-img {
		width: 120rpx;
		height: 120rpx;
		border-radius: 4rpx;
	}

	.meal-list-content {
		margin-left: 28rpx;
		flex: 1;
	}

	.meal-items {
		display: flex;
		align-items: flex-end;
		justify-content: space-between;
	}

	.meal-list-name {
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: rgba(34, 34, 34, 1);
		line-height: 1;
	}

	.meal-list-time {
		margin-top: 26rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(38, 141, 255, 1);
	}

	.meal-list-price {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 85, 0, 1);
		line-height: 1;
	}

	.flex-content {
		position: fixed;
		bottom: 0;
		padding: 0rpx 30rpx 60rpx 30rpx;
		background-color: #ffffff;
	}

	.defray-count {
		width: 530rpx;
		height: 98rpx;
		background: rgba(51, 51, 51, 1);
		box-shadow: 0rpx 5rpx 10rpx 0rpx rgba(0, 0, 0, 0.2);
		border-radius: 49rpx 0rpx 0rpx 49rpx;
		display: flex;
		align-items: center;
		padding-left: 40rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
		box-sizing: border-box;
	}

	.defray-button {
		width: 160rpx;
		height: 98rpx;
		background: linear-gradient(0deg, rgba(255, 195, 36, 1), rgba(255, 211, 94, 1));
		box-shadow: 0rpx 5rpx 10rpx 0rpx rgba(0, 0, 0, 0.2);
		border-radius: 0rpx 49rpx 49rpx 0rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: rgba(51, 51, 51, 1);
	}

	.count-number {
		margin-left: 20rpx;
		color: #999999;
	}
</style>
